﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartEdit
{
    public enum SEMode
    {
        NotPressed,
        Moving,
    }
    public class SubEditor
    {
        private Editor ed;
        private bool moving;
        private int iOffset;//initial offset
        private int fOffset;//final offset
        private int iQuant;//initial quantization
        private List<KMKey> keys;
        private List<KMKey> keys2;
        private List<KMKey> keysLocked = new List<KMKey>();
        public List<Note> GhostNotes = new List<Note>();
        public void Think(bool keyTrigger, bool keyChanged)
        {
            keys2 = ed.KeysPressed;
            switch (ed.MainTool)
            {
                default:
                    keys = ed.KeysPressed;
                    break;
                case(MTool.Arpeggiator):
                    keys = ed.KeysPressedOrdered;
                    break;
            }
            if (keys.Count() == 0)
            {
                if (keysLocked.Count() > 0)
                {
                    addNotes();
                    keysLocked.Clear();
                }
                moving = false;
                iOffset = ed.QOffset;
                fOffset = ed.QOffset;
                iQuant = ed.Quantization;
            }
            else
            {
                fOffset = ed.QOffset;

                if (!moving)
                {
                    if (fOffset != iOffset)
                        moving = true;
                    switch(ed.MainTool)
                    {
                        default:
                            foreach (KMKey k in keys)
                            {
                                if (k.Type != KeyType.Note)
                                    continue;
                                if (!keysLocked.Contains(k))
                                    keysLocked.Add(k);
                            }
                            break;
                        case(MTool.Arpeggiator):
                            if (keysLocked.Count() != keys.Count())
                            {
                                keysLocked.Clear();
                                foreach (KMKey k in keys)
                                {
                                    if (k.Type != KeyType.Note)
                                        continue;
                                    keysLocked.Add(k);
                                }
                            }
                            break;
                    }
                    
                }

                foreach(KMKey k in keys2)
                {
                    if (!keyTrigger)
                        continue;
                    if (k.Type == KeyType.MoveUp)
                        ed.Shift(ed.Quantization);
                    if (k.Type == KeyType.MoveDown)
                        ed.Shift(-ed.Quantization);
                    if (!keyChanged)
                        continue;
                    if (k.Type == KeyType.Play)
                    {
                        if (ed.song.IsPlaying())
                            ed.song.Pause();
                        else
                            ed.song.Play();
                    }
                }
            }

            createGhostNotes();
        }
        private void createGhostNotes()
        {
            switch(ed.MainTool)
            {
                default:
                    GhostNotes.Clear();
                    foreach (KMKey k in keysLocked)
                    {
                        if (k.Type == KeyType.Note)
                        {
                            GhostNotes.Add(new Note(Globals.Instance.GameProperties[0].
                                TrackProperties[ed.noteTrack.Name].NoteProperties[k.Value]));
                            GhostNotes.Last().Offset = iOffset;
                            GhostNotes.Last().OffsetEnd = fOffset;
                        }
                    }
                    break;
                case(MTool.Arpeggiator):
                    GhostNotes.Clear();
                    if (keysLocked.Count() == 0)
                        break;
                    int count = (fOffset - iOffset) / iQuant;
                    KMKey key;
                    for (int i = 0; i < count; i++)
                    {
                        key = keysLocked[i % keysLocked.Count()];
                        GhostNotes.Add(new Note(Globals.Instance.GameProperties[0].
                            TrackProperties[ed.noteTrack.Name].NoteProperties[key.Value]));
                        GhostNotes.Last().Offset = iOffset+iQuant*i;
                        GhostNotes.Last().Length = 0;
                    }
                    break;
                case(MTool.Tremolo):
                    break;
                case(MTool.Pattern):
                    break;
            }
        }

        private void addNotes()
        {
            if (ed.noteTrack == null)
                return;
            createGhostNotes();
            ed.AddNotes(GhostNotes);
        }
        //Constructor
        public SubEditor(Editor ed)
        {
            this.ed = ed;
        }
    }
}
